import { useAsync } from "utils/use-async";
import { renderHook } from "@testing-library/react-hooks";
import { act } from "react-dom/test-utils";
const defaultState: ReturnType<typeof useAsync> = {
  stat: "idle",
  data: null,
  error: null,
  isIdle: true,
  isLoading: false,
  isError: false,
  isSuccess: false,
  run: expect.any(Function),
  setData: expect.any(Function),
  setError: expect.any(Function),
  retry: expect.any(Function),
};
const loadingState: ReturnType<typeof useAsync> = {
  ...defaultState,
  stat: "loading",
  isIdle: false,
  isLoading: true,
};
const successStat: ReturnType<typeof useAsync> = {
  ...defaultState,
  stat: "success",
  isIdle: false,
  isSuccess: true,
};
test("useAsync可以处理异步", async () => {
  let resolve: any;
  const promise = new Promise((res) => {
    resolve = res;
  });
  const { result } = renderHook(() => useAsync());
  expect(result.current).toEqual(defaultState);
  let p: Promise<any>;
  act(() => {
    p = result.current.run(promise);
  });
  expect(result.current).toEqual(loadingState);
  const resolvedValue = { mockValue: "mock" };
  await act(async () => {
    resolve(resolvedValue);
    await p;
  });
  expect(result.current).toEqual({ ...successStat, data: resolvedValue });
});
